package data;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class DBLoader {
	public static Database getDB(String path) {
		Database db = null;
		try {
			InputStream file = new FileInputStream(path);
			InputStream buffer = new BufferedInputStream(file);
			ObjectInput input = new ObjectInputStream (buffer);
			try {
				db = (Database)input.readObject();
			} finally {
				input.close();
			}
		} catch (ClassNotFoundException ex) {
			System.err.println("Loader: Class not found");
		} catch (IOException ex) {
			System.err.println("Loader: Can't do input.");
			ex.printStackTrace();
		}
		
		return db;
	}
	
	//save
	public static void saveDB(Database db, String path) {
		try {
			OutputStream file = new FileOutputStream(path);
			OutputStream buffer = new BufferedOutputStream(file);
			ObjectOutput output = new ObjectOutputStream(buffer);
			try {
				output.writeObject(db);
			} finally {
				output.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
